' Volt Meter Test

SetPin 5,ain ' analog in
Font 1,2
Text 10,180,"Micromite 4.7 Beta8",,1,2,RGB(cyan),RGB(black)
Text 10,210,"ILI9341 "+Str$(MM.HRes)+"x"+Str$(MM.VRes)+" DSP",,1,2,RGB(cyan),RGB(black)
RBox 10,10,120,50,3,RGB(yellow)
Font 1,1
Text 15,25,"Increse Value",,,,RGB(green),RGB(black)
RBox 10,70,120,50,3,RGB(yellow)
Text 15,85,"Decrese Value",,,,RGB(green),RGB(black)
RBox 10,130,120,48,3,RGB(yellow)
Text 15,145,"Pin 5 Volts",,,,RGB(green),RGB(black)
MeterName$="Demo Meter"
plot_scale(230,120,20)
plot_needle(230,120,i,20)

Do
If Touch(x)>=10 And Touch(x)<=120 And Touch(y)>=15 And Touch(y)<=55 Then
  Print "Increse Button"
  Print Touch(x); Touch(y)
  If i<20 Then i=i+1
  plot_needle(230,120, i, 20)
  RBox 10,10,120,50,3,RGB(yellow),RGB(yellow)
  Do
  Loop Until Touch(x)=-1 And Touch(y)=-1
  RBox 10,10,120,50,3,RGB(yellow),RGB(black)
  Text 15,25,"Increse Value",,,,RGB(green),RGB(black)
EndIf

If Touch(x)>=10 And Touch(x)<=120 And Touch(y)>=70 And Touch(y)<=100 Then
  Print "Decrese Button"
  Print Touch(x); Touch(y)
  If i>0 Then i=i-1
  plot_needle(230,120, i, 20)
  RBox 10,70,120,50,3,RGB(yellow),RGB(yellow)
  Do
  Loop Until Touch(x)=-1 And Touch(y)=-1
  RBox 10,70,120,50,3,RGB(yellow),RGB(black)
  Text 15,85,"Decrese Value",,,,RGB(green),RGB(black)
EndIf

If Touch(x)>=10 And Touch(x)<=120 And Touch(y)>=130 And Touch(y)<=180 Then
  Print "Volts Button"
  Print Touch(x); Touch(y)
    If cycle_running Then
      SetTick 0,0
      cycle_running=0
      i=0
      plot_needle(230,120, i, 20)
    Else
      SetTick 100, read_v
      cycle_running=1
    EndIf
  RBox 10,130,120,48,3,RGB(yellow),RGB(yellow)
  Do
  Loop Until Touch(x)=-1 And Touch(y)=-1
  RBox 10,130,120,48,3,RGB(yellow),RGB(black)
  Text 15,145,"Pin 5 Volts",,,,RGB(green),RGB(black)
EndIf
Loop

Sub read_v
voltage$=Mid$(Str$(Pin(5)),1,4)
i=Val(voltage$)*6
plot_needle(230,120, i, 20)
Print voltage$
End Sub

Sub next_pos
If Not inc_val And i>0 Then i=i-.1: plot_needle(230,120, i, 20)
If i<=0 Then inc_val=1: i=0: plot_needle(230,120, i, 20)
If inc_val And i<20 Then i=i+.1: plot_needle(230,120, i, 20)
If i>=20 Then inc_val=0: i=20: plot_needle(230,120, i, 20)
v$=Str$(i,2,1)
i=Val(v$)
EndIf
End Sub

Sub plot_scale (center_x, center_y, max_s)
  Box center_x-84,center_y-120,center_x+84,center_y+20,1,RGB(yellow)
  Font 1
  Color RGB(white)
  For i= 0 To 20
    rads=(i-10)*Pi/40
    x2=Sin(rads)*100 + center_x
    y2=-Cos(rads)*100 + center_y
    If i Mod 4 <> 0 Then
      x1=Sin(rads)*95 + center_x
      y1=-Cos(rads)*95 + center_y
    Else
      x1=Sin(rads)*90 + center_x
      y1=-Cos(rads)*90 + center_y
      Text x2-2,y2-15,Str$(max_s*i/20) ' ,r,1,1
    EndIf
    Line x1,y1,x2,y2,1,RGB(yellow)
  Next i
  Font 1
  Color RGB(Yellow)
  Text center_x-84,center_y+25,MeterName$
End Sub

Sub plot_needle (center_x, center_y, needle_v, max_s)
  Local my_needle
  my_needle= needle_v * 100 / max_s
  ' needle value 0..100
  If my_needle <> needle_old Then ' Erase old needle POS
    Circle center_x,center_y,8,3,1,RGB(yellow),RGB(red)
    rads= (needle_old-50)*Pi/200
    x2=Sin(rads)*90 + center_x
    y2=-Cos(rads)*90 + center_y
    Line center_x,center_y,x2,y2,1,RGB(black)
    rads= (my_needle-50)*Pi/200 ' Draw new needle pos
    x2=Sin(rads)*90 + center_x
    y2=-Cos(rads)*90 + center_y
    Line center_x,center_y,x2,y2,1,RGB(Red)
    Font 1,2
    Text center_x+5,center_y+30,Str$(needle_v,3,1),,,,RGB(green),RGB(black)
    Font 1
  EndIf
  needle_old = my_needle
End Sub                                                                                                             